package com.jichangxiu.system.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.system.entity.vo.SysMenuVo;
import com.jichangxiu.system.entity.vo.SysPowerVo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MenuUtils:
 *
 * @author Jcx
 * @create 2023-11-26 20:56
 */
public class MenuUtils {

    /**
     * 菜单业务对象 转换为 Record
     *
     * @param vo 菜单视图对象
     * @return Record
     */
    public static Record toRecord(SysMenuVo vo) {
        Record record = Record.getRecord();
        record.set("id", vo.getId());
        record.set("parentId", vo.getParentId());
        record.set("no", vo.getNo());

        record.set("path", StrUtil.isNotEmpty(vo.getPath()) ? vo.getPath() : "/");
        record.set("name", vo.getCode());
        record.set("component", vo.getComponent());

        Record meta = Record.getRecord();
        meta.set("title", vo.getName());
        meta.set("icon", vo.getIcon());
        meta.set("sort", vo.getSort());
        meta.set("hidden", vo.getHidden());
        meta.set("showParent", true);
        meta.set("extraIcon", "");

        meta.set("type", vo.getType());
        meta.set("category", vo.getCategory());
        meta.set("dynamicLevel", "");
        meta.set("hiddenTag", false);
        meta.set("affix", false);
        meta.set("keepAlive", false);
        meta.set("frameLoading", false);
        meta.set("fullPage", false);

        List<SysPowerVo> sysPowerVoList = vo.getSysPowerVoList();
        if (ObjectUtil.isNotEmpty(sysPowerVoList)) {
            meta.set("auths", sysPowerVoList.stream().map(SysPowerVo::getCode).collect(Collectors.toList()));
            meta.set("roles", new ArrayList<>());
        } else {
            meta.set("auths", new ArrayList<>());
            meta.set("roles", new ArrayList<>());
        }

//        Record transition = Record.getRecord();
//        transition.set("name", "fade");
//         transition.set("enterTransition", "animate__pulse");
//         transition.set("leaveTransition", "animate__pulse");
//        meta.set("transition", transition);

        record.set("meta", meta);
        return record;
    }

    /**
     * 菜单视图对象列表列表 转化为 Record
     *
     * @param sysMenuVoList 菜单视图对象列表
     * @return Record
     */
    public static List<Record> listToRecord(List<SysMenuVo> sysMenuVoList) {
        // 升序
        sysMenuVoList = sysMenuVoList.stream().sorted(Comparator.comparing(SysMenuVo::getSort)).collect(Collectors.toList());
        List<Record> recordList = new ArrayList<>();
        sysMenuVoList.forEach(sysMenuVo -> recordList.add(toRecord(sysMenuVo)));
        return recordList;
    }

}
